package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 *
 */
@Service
@Transactional
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    public ResponseResult findByPageAndName(ChannelDto dto){
        // 检查参数
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        dto.checkParam();

        // 构建条件
        Page channelPage = new Page(dto.getPage(),dto.getSize());
        // 判断是否有条件
        LambdaQueryWrapper<AdChannel> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())){
            wrapper.like(AdChannel::getName,dto.getName());
        }
        IPage pageResult = page(channelPage, wrapper);
        // 封装响应结果
        PageResponseResult pageResponseResult =
                new PageResponseResult(dto.getPage(),dto.getSize(),(int)pageResult.getTotal());
        pageResponseResult.setData(pageResult.getRecords());

        return pageResponseResult;
    }

    @Override
    public ResponseResult insert(AdChannel channel) {
        // 检查参数，姓名非空
        if (channel == null || StringUtils.isBlank(channel.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (channel.getIsDefault() == null){
            channel.setIsDefault(false);
        }

        // 判断表中有没有默认频道，有则将其变成非默认频道
        if (channel.getIsDefault()){
            // 如果是默认频道，判断表中是否还有默认频道
            LambdaQueryWrapper<AdChannel> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(AdChannel::getIsDefault,1);
            AdChannel one = getOne(wrapper);

            if (one != null){
                one.setIsDefault(false);
                updateById(one);
            }
        }
        // 添加日期
        channel.setCreatedTime(new Date());
        // 新增频道
        save(channel);
        return ResponseResult.okResult();

    }

    @Override
    public ResponseResult updateChannel(AdChannel channel) {
        // 检查参数
        if (channel == null || StringUtils.isBlank(channel.getName()) || channel.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        // 检查参数成功后，修改数据
        updateById(channel);

        return ResponseResult.okResult();

    }

    @Override
    public ResponseResult deleteById(Integer id) {
        // 检查参数
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        // 判断该条记录是否已经失效,如果失效就删除,如果还未失效则直接返回
        AdChannel channel = getById(id);
        if (channel.getStatus()){
            // 还没失效
            return ResponseResult.errorResult(AppHttpCodeEnum.STATUS_USED);
        }
        // 失效了直接删除
        // 制造异常
//        int a = 1/0;
        // 测试成功
        removeById(id);

        return ResponseResult.okResult();
    }

}
